package org.dromara.company.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.BCrypt;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.admin.domain.bo.EmployeeBo;
import org.dromara.admin.domain.model.UserPasswordBo;
import org.dromara.admin.domain.vo.EmployeeVo;
import org.dromara.admin.service.IEmployeeService;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.helper.DataPermissionHelper;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.common.web.core.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 公司端员工信息
 *
 * @author Lion Li
 * @date 2025-10-10
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/company/employee")
public class EmployeeController extends BaseController {

    private final IEmployeeService employeeService;

    /**
     * 分页查询当前登录用户员工信息列表
     */
//    @SaCheckPermission("system:employee:list")
    @GetMapping("/myList")
    public TableDataInfo<EmployeeVo> myList(EmployeeBo bo, PageQuery pageQuery) {
        Long userId = LoginHelper.getUserId();
        if (userId == null) {
            throw new ServiceException("请先登录！");
        }
        bo.setId(userId);
        return employeeService.queryPageMyList(bo, pageQuery);
    }

    /**
     * 导出员工信息列表
     */
//    @SaCheckPermission("system:employee:export")
    @Log(title = "员工信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(EmployeeBo bo, HttpServletResponse response) {
        List<EmployeeVo> list = employeeService.queryList(bo);
        ExcelUtil.exportExcel(list, "员工信息", EmployeeVo.class, response);
    }

    /**
     * 获取员工详细信息含主体信息
     */
//    @SaCheckPermission("system:employee:query")
    @GetMapping("/getInfoAndCompany")
    public R<EmployeeVo> getInfoAndCompany() {
        Long id = LoginHelper.getUserId();
        if (id == null) {
            return R.fail("请先登录！");
        }
        return R.ok(employeeService.queryById(id));
    }

    /**
     * 修改员工信息
     */
//    @SaCheckPermission("system:employee:edit")
    @Log(title = "员工信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody EmployeeBo bo) {
        return toAjax(employeeService.updateByBo(bo));
    }

    /**
     * 修改员工账号状态
     */
//    @SaCheckPermission("system:employee:edit")
    @Log(title = "员工信息", businessType = BusinessType.UPDATE)
    @PutMapping("/changeEmployeeStatus/{userId}")
    public R<Void> changeEmployeeStatus(@PathVariable Long userId) {
        return toAjax(employeeService.auditRegister(userId));
    }

    /**
     * 修改密码
     */
    @Log(title = "员工信息", businessType = BusinessType.UPDATE)
    @PutMapping("/resetPwd")
    public R<Void> resetPwd(@Validated @RequestBody UserPasswordBo userPasswordBo) {
        // 判断是否传递了userId
        if (userPasswordBo.getUserId() == null) {
            // 如果没有传递userId，则使用当前登录用户，修改当前登录用户的密码
            userPasswordBo.setUserId(LoginHelper.getUserId());
        }
        userPasswordBo.setEncryptPassword(BCrypt.hashpw(userPasswordBo.getNewPassword()));
        int rows = DataPermissionHelper.ignore(() -> employeeService.resetUserPwd(userPasswordBo));
        if (rows > 0) {
            return R.ok();
        }
        return R.fail("修改密码异常，请联系管理员!");
    }
}
